#include "thread_pool.h"

#include <chrono>

ThreadPool::ThreadPool(size_t threads_n) {
  for (size_t i = 0; i < threads_n; ++i) {
    workers_.emplace_back([this] {
      for (;;) {
        TaskType task;
        if (queue_.DequeueWaitFor(task, std::chrono::seconds(3))) {
          task();
        }
      }
    });
  }
}

ThreadPool::~ThreadPool() {
  queue_.Cancel();

  for (auto &workder : workers_) {
    workder.join();
  }
}
